#!/bin/bash

for i in {1..3}; do
  # Check disk useage
  DISK_USED=`df /backup/ | tail -n1 | awk '{print $5}' | sed 's/%//g'`

  # Free some disk space before backup
  if [ $(($DISK_USED)) -gt {{ combined_vars.xtrabackup.max_disk_used }} ]; then
    cd /backup/
    ls -tr | tail -n1 | xargs rm -rf
  else
    break
  fi
done

# Start backup
xtrabackup \
  --backup \
  --compress=zstd \
  --compress-zstd-level=9 \
  --compress-threads=4 \
  --target_dir=/backup/`date -Iseconds`

# How to restore?
echo "Read doc from https://docs.percona.com/percona-xtrabackup/8.0/backup_scenarios/full_backup.html#prepare-a-backup to know how to restore backup."